﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Threading;

namespace MulticastTV
{
    /// <summary>
    /// Export group and language information for every channel to xml.
    /// This xml is then used for importing m3u playlist which doesn't contain any group or language informations.
    /// </summary>

    class ExportGroups
    {
        char[] plusdelimiter = { '+' };
        static string AppPath = System.Windows.Forms.Application.CommonAppDataPath;
        public void Start()
        {
            DataTable groupst = new DataTable();

            groupst.Columns.Add("Name", typeof(string));
            groupst.Columns.Add("Group", typeof(string));
            groupst.Columns.Add("Audio", typeof(string));
            groupst.Columns["Name"].DefaultValue = "";
            groupst.Columns["Group"].DefaultValue = "";
            groupst.Columns["Audio"].DefaultValue = "";
            groupst.PrimaryKey = new DataColumn[] { groupst.Columns["Name"] };
            groupst.TableName = Thread.CurrentThread.CurrentCulture.IetfLanguageTag;

            foreach (DataRow dr in ChannelTable.menu.Tables["Menu"].Rows)
            {
                DataRow menurow;
                menurow = groupst.NewRow();
                menurow["Name"] = dr["Name"];
                menurow["Group"] = dr["Group"];
                menurow["Audio"] = dr["Audio"];
   
                bool exists=false;
                foreach (DataRow drd in groupst.Rows)
                {
                    if (string.Equals(drd["Name"].ToString().Trim(), menurow["Name"].ToString().Trim(), StringComparison.CurrentCultureIgnoreCase) )
                    {
                        exists = true;
                        break;
                    }
                }
                
                if (!exists)
                    groupst.Rows.Add(menurow);

            }
            groupst.WriteXml(AppPath + @"\..\groups_" + Thread.CurrentThread.CurrentCulture.IetfLanguageTag + "_utf8.xml");
        }
    }
}
